﻿using Business.Service;
using Model;
using MvcAppWeb.Controllers;
using Public;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using WoChu.Infrastructure.Data;
using WoChu.Infrastructure.Error;
using WoChu.Infrastructure.ModelCommmon;
using WoChu.Infrastructure.Web;

namespace MvcAppWeb.Controllers
{
    public class PermissionManageController : BaseDataController
    {
        UserService _service = new UserService();

        #region 用户配置  相关操作
        public ActionResult UserManage()
        {
            ViewBag.IsAdminUser = this.IsAdminUser;
            return View();
        }

        public ActionResult GetUserList(AppUserList model)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            
            List<SqlCondition> conditions=new List<SqlCondition>() { 
                new SqlCondition("USERNAME", OperateType.Like) };
            bool isEmpty = false;
            if(IsAdminUser)
                conditions.Add(new SqlCondition("USERCODE", OperateType.Like));
            else
            {
                conditions.Add( new SqlCondition("USERCODE"));
                if (!string.IsNullOrEmpty(model.USERCODE) && this.CurrentUser.USERCODE.IndexOf(model.USERCODE) < 0)
                    isEmpty = true;
                model.USERCODE = this.CurrentUser.USERCODE;
            }
            PageInfo<AppUserList, AppUserList> pageInfo = new PageInfo<AppUserList, AppUserList>()
            {
                Condition = model,
                PageIndex = page,
                PageSize = rows,
                Conditions = conditions,
            };
            if (!isEmpty)
                _service.GetUserList(pageInfo);
            return Json(pageInfo);
        }

        [HttpPost]
        public ActionResult SaveUser(AppUserList model)
        {
            var ajaxRet = new AjaxResult();
            try
            {
                int ret = 0;
                model.LASTMODITIME = DateTime.Now;
                model.LASTMODIOPID = this.CurrentUser.USERNAME;
                if (model.USERID < 1)     //新增
                    ret = _service.Insert(model);
                else
                    ret = _service.Update(model);

                if (ret < 1)
                {
                    new BussinessException("错误：影响行0").Throw();
                }
                return Json(ajaxRet.SetSuccess("操作成功"));
            }
            catch (Exception ex)
            {
                _Log.LogError(ex, "PermissionManageController.SaveUser");
                return Json(ajaxRet.SetError(ex, "操作失败"));
            }
        }


        [HttpPost]
        public ActionResult DeleteUser(AppUserList model)
        {
            var ajaxRet = new AjaxResult();
            try
            {
                int ret = _service.Delete(model.USERID);

                if (ret < 1)
                {
                    new BussinessException("错误：影响行0").Throw();
                }
                return Json(ajaxRet.SetSuccess("操作成功"));
            }
            catch (Exception ex)
            {
                _Log.LogError(ex, "PermissionManageController.Insert");
                return Json(ajaxRet.SetError(ex, "操作失败"));
            }
        }

        public ActionResult ExistsUserCode(AppUserList model)
        {
            bool exists = _service.Exists(model);
            
            ContentResult cr = new ContentResult();
            cr.Content = (!exists).ToString().ToLower();
            return cr;
        }
        #endregion


    }
}